using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CS6238Project2;

namespace CS6238Project2.GUI
{
    public partial class NetworkControlForm : Form
    {

        
        public NetworkControlForm()
        {
            InitializeComponent();
        }


        //Listening Handler
        public delegate void NetworkControlHandler(
                object sender, NetworkControlEventArgs e);

        public event NetworkControlHandler NetworkEvent;


        private void listeningPortButton_Click(object sender, EventArgs e)
        {
            NetworkControlEventArgs args =
                new NetworkControlEventArgs(listeningPortTextBox.Text, 
                sendingPortTextBox.Text, connectionButton.Text, ipAddressTextBox.Text);
            
            NetworkEvent(this, args);
        }


        private void sendingPortButton_Click(object sender, EventArgs e)
        {
            NetworkControlEventArgs args =
                new NetworkControlEventArgs(sendingPortTextBox.Text, 
                sendingPortTextBox.Text, connectionButton.Text, ipAddressTextBox.Text);

            NetworkEvent(this, args);
        }


        private void ConnectionPortButton_Click(object sender, EventArgs e)
        {
            NetworkControlEventArgs args;


            if (connectionButton.Text == "Disconnected")
            {
                connectionButton.Text = "Connected";
                args = new NetworkControlEventArgs(listeningPortTextBox.Text,
                sendingPortTextBox.Text, connectionButton.Text, ipAddressTextBox.Text);
            }
            else
            {
                connectionButton.Text = "Disconnected";
                args = new NetworkControlEventArgs(listeningPortTextBox.Text,
                sendingPortTextBox.Text, connectionButton.Text, null);
            }

            NetworkEvent(this, args);
        }



        

    }

    public class NetworkControlEventArgs : System.EventArgs
    {
        private int listeningPort;
        private int sendingPort;
        private bool connected;
        private System.Net.IPAddress ipAddress;


        public NetworkControlEventArgs(string listen, string send, string connect, string ip)
        {
            try
            {
                listeningPort = Convert.ToInt32(listen);
                sendingPort = Convert.ToInt32(send);
                connected = !(connect == "Connected");
                ipAddress = System.Net.IPAddress.Parse(ip);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        public int ListeningPort
        {
            get { return listeningPort; }
        }
        public int SendingPort
        {
            get { return sendingPort; }
        }
        public bool Connected
        {
            get { return connected; }
        }

    }
}